new Handle:g_MapEndVote = INVALID_HANDLE
new Handle:g_VoteBegin = INVALID_HANDLE
new Handle:g_NextMap = INVALID_HANDLE
new bool:IsNextMap = false
new SecsLeft = 0

public OnEndMap(Handle:event, const String:name[], bool:dontBroadcast)
{
	if (Debug)
		PrintToServer("pre hooked succesfully")

	if (GetConVarInt(g_MapEndVote) == 1)
	{
		CreateTimer(1.0, DO_Mapvote)
		return
	}
	return
}

public Action:DO_Mapvote(Handle:timer)
{
	ServerCommand("sm_mapvote")
	return Plugin_Handled
}

public g_MapEnd(Handle:convar, const String:oldValue[], const String:newValue[])
{
	if (GetConVarInt(g_MapEndVote) == 1)
	{
		CreateTimer(30.0, ClearNextMap)
		
		SetConVarInt(FindConVar("mp_chattime"), 35)
		SetConVarInt(FindConVar("sm_mapvote_endvote"), 0)
		SetConVarInt(FindConVar("sm_mapvote_extend"), 0)
		SetConVarInt(FindConVar("sm_mapvote_voteduration"), 20)
	}
	else
	{
		SetConVarInt(FindConVar("mp_chattime"), 10)
		SetConVarInt(FindConVar("sm_mapvote_endvote"), 1)
		SetConVarInt(FindConVar("sm_mapvote_extend"), 1)
		SetConVarInt(FindConVar("sm_mapvote_voteduration"), 20)
	}
}

public OnNextMapChange(Handle:convar, const String:oldValue[], const String:newValue[])
{
	decl String:NextMap[64]
	
	if (!CheckMapEnd)
	{
		GetNextMap(NextMap,sizeof(NextMap))
		PrintToChatAll("\x01\x04[SYN] Next map was set: %s", NextMap)
		PrintToChatAll("\x01\x04[SYN] Map end vote disabled!", NextMap)
		PrintToServer("[SYN] Next map was set: %s", NextMap)
		PrintToServer("[SYN] Map end vote disabled!", NextMap)
		SetConVarInt(g_MapEndVote, 0)
	}
}

public Action:ClearNextMap(Handle:timer)
{
	decl String:NextMap[64]
	
	g_NextMap = FindConVar("sm_nextmap")
	if (g_NextMap != INVALID_HANDLE)
		HookConVarChange(g_NextMap, OnNextMapChange)

	if (GetNextMap(NextMap,sizeof(NextMap)))
	{
		IsNextMap = true

		PrintToChatAll("\x01\x04[SYN] Next map cleared.")
		PrintToServer("[SYN] Next map cleared.")
	}
	else
	{
		IsNextMap = false

		PrintToChatAll("\x01\x04[SYN] Next map already cleared.")
		PrintToServer("[SYN] Next map already cleared.")
		return Plugin_Handled
	}
	return Plugin_Handled
}

public Action:CheckLeft(Handle:timer)
{
	GetMapTimeLeft(SecsLeft)
	if (SecsLeft==GetConVarInt(g_VoteBegin)*60){
		CreateTimer(1.0, VoteExtend)
	} else {
		CreateTimer(1.0, CheckLeft)
	}
}

public Action:VoteExtend(Handle:timer)
{
	ServerCommand("sm_cvote extend")
	if (GetConVarInt(g_VoteExtend) == 1)
		CreateTimer(5.0, CheckLeft)
}